<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-copy"></i> 我的消息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-tabs v-model="message" style="width:1200px">
                <el-tab-pane :label="`未读消息(${unread.length})`" name="first">
                    <el-table :data="unread" :show-header="false" style="width: 100%">
                        <el-table-column>
                            <template slot-scope="scope">
                                <span class="message-title">{{ scope.row.content }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="createDate" width="180"></el-table-column>
                        <el-table-column width="120">
                            <template slot-scope="scope">
                                <el-button size="small" @click="handleRead(scope)">标为已读</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="handle-row">
                        <el-button type="primary">全部标为已读</el-button>
                    </div>
                </el-tab-pane>
                <el-tab-pane :label="`已读消息(${read.length})`" name="second">
                    <template v-if="message === 'second'">
                        <el-table :data="read" :show-header="false" style="width: 100%">
                            <el-table-column>
                                <template slot-scope="scope">
                                    <span class="message-title">{{ scope.row.content }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="createDate" width="150"></el-table-column>
                            <el-table-column width="120">
                                <template slot-scope="scope">
                                    <el-button type="primary" @click="handleUnRead(scope)">标为未读</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                </el-tab-pane>
                
            </el-tabs>
        </div>
    </div>
</template>

<script>
export default {
    name: 'tabs',
    data() {
        return {
            message: 'first',
            showHeader: false,
            unread: [],
            read: []
        };
    },
    created(){
        this.$http.get("/message/mymessage").then(res => {
            this.unread = res.data.data.unread;
            this.read = res.data.data.read;
        });
    },
    methods: {
        handleRead(scope) {
            this.unread.splice(scope.$index, 1);
            var message = scope.row;
            message.isRead = 1;
            this.$http.get("/message/updateisread?id="+message.id+"&isRead="+1).then(res => {
                this.read = this.read.concat(message);
            });
            
        },
        handleUnRead(scope){
            this.read.splice(scope.$index, 1);
            var message = scope.row;
            message.isRead = 0;
            this.$http.get("/message/updateisread?id="+message.id+"&isRead=" + 0).then(res => {
                this.unread = this.unread.concat(message);
            });
        }
    },
    computed: {
        unreadNum() {
            return this.unread.length;
        }
    }
};
</script>

<style>
.message-title {
    cursor: pointer;
    font-size: 15px;
}
.handle-row {
    margin-top: 30px;
}
</style>

